<template>
  <div class="changePsw">
    <el-form
      ref="passwordform"
      :model="passwordform"
      :rules="rulespassword"
      label-width="100px"
      class="forms"
    >
      <el-form-item label="旧密码" prop="oldPassword" type="password">
        <el-input
          type="password"
          style="position: fixed; bottom: -9999px"
        ></el-input>
        <el-input
          type="password"
          placeholder="请输入旧密码"
          v-model="passwordform.oldPassword"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          type="password"
          style="position: fixed; bottom: -9999px"
        ></el-input>
        <el-input
          placeholder="请输入新密码"
          v-model="passwordform.newPassword"
          type="password"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="surePwd">
        <el-input
          placeholder="请再次输入密码"
          v-model="passwordform.surePwd"
          type="password"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitPwd('passwordform')"
          >确认修改</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updatePwd } from "../../apis/index";
export default {
  name: "changePsw",
  components: {},
  data() {
    return {
      // 修改密码
      passwordform: {
        oldPassword: "",
        newPassword: "",
        surePwd: "",
      },
      // 表单效验规则
      rulespassword: {
        oldPassword: [
          { required: true, message: "请输入旧密码", trigger: "blur" },
        ],
        newPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
        ],
        surePwd: [
          { required: true, message: "请再次输入新密码", trigger: "blur" },
          //调用validateSurePwd方法，将新密码和确认密码进行效验
          { validator: this.validateSurePwd, trigger: "blur" },
        ],
      },
    };
  },
  computed: {},
  created() {},
  methods: {
    // 修改密码
    //点击确定按钮提交
    submitPwd(ele) {
      this.$refs[ele].validate(async (valid) => {
        if (valid) {
          updatePwd(this.passwordform)
            .then((res) => {
              console.log(res, 7777777777);

              if (res.data.code == 200) {
                this.$message({
                  message: res.data.msg,
                  type: "success",
                });
                this.passwordform = {
                  oldPassword: "",
                  newPassword: "",
                  surePwd: "",
                };
                setTimeout(()=>{
                  this.$router.push('/login')
                },1500)
                
              } else {
                this.$message({
                  message: res.data.msg,
                  type: "warning",
                });
              }
            })
            .catch(() => {});
        }
      });
    },

    //确认密码和新密码的动态效验
    validateSurePwd(rule, value, callback) {
      if (value === "") {
        callback(new Error("请再次输入新密码"));
      } else if (value !== this.passwordform.newPassword) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.forms {
  width: 500px;
  margin: 50px 0;
}
.changePsw {
}
</style>